package top.csmcool.musicapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import top.csmcool.musicapi.config.SystemProperties;
import top.csmcool.musicapi.domain.UserPlatformBindInfo;
import top.csmcool.musicapi.domain.Users;
import top.csmcool.musicapi.enums.StatusCode;
import top.csmcool.musicapi.model.admin.PageVO;
import top.csmcool.musicapi.model.admin.SystemSetting;
import top.csmcool.musicapi.model.admin.UserPlatformBindInfoPageRequest;
import top.csmcool.musicapi.model.admin.UsersRequest;
import top.csmcool.musicapi.model.result.Request;
import top.csmcool.musicapi.model.result.Result;
import top.csmcool.musicapi.service.AdminService;
import top.csmcool.musicapi.service.UserPlatformBindInfoService;
import top.csmcool.musicapi.service.UsersService;
import top.csmcool.musicapi.util.ResultUtil;

/**
 * @Description
 * @Author shizhe
 * @Date 2025/2/13
 */

@Service
@RequiredArgsConstructor
public class AdminServiceImpl implements AdminService {

    private final UserPlatformBindInfoService userPlatformBindInfoService;
    private final UsersService usersService;
    private final SystemProperties systemProperties;

    @Override
    public Result<PageVO<UserPlatformBindInfo>> queryUserPlatformBindInfo(Request<UserPlatformBindInfoPageRequest> request) {
        UserPlatformBindInfoPageRequest body = request.getBody();
        Page<UserPlatformBindInfo> userPlatformBindInfoPage = new Page<>(body.getPage(), body.getSize());
        QueryWrapper<UserPlatformBindInfo> queryWrapper = new QueryWrapper<>();
        if(body.getUid() != null) {
            queryWrapper.eq("user_id",body.getUid());
        }

        if(body.getUsername() != null) {
            queryWrapper.eq("username", body.getUsername());
        }
        Page<UserPlatformBindInfo> page = userPlatformBindInfoService.page(userPlatformBindInfoPage, queryWrapper);
        PageVO<UserPlatformBindInfo> userPageVO = new PageVO<>();
        BeanUtils.copyProperties(page,userPageVO);
        return ResultUtil.success(userPageVO);
    }

    @Override
    public Result<PageVO<Users>> queryUsers(Request<UsersRequest> request) {
        UsersRequest body = request.getBody();
        Page<Users> page = new Page<>(body.getPage(), body.getSize());

        PageVO<Users> userPageVO = new PageVO<>();
        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();

        if(body.getUid() != null) {
            queryWrapper.eq("id", body.getUid());
        }

        if(body.getEmail() != null) {
            queryWrapper.eq("email", body.getEmail());
        }

        if(body.getUsername() != null) {
            queryWrapper.eq("username", body.getUsername());
        }

        Page<Users> usersPage = usersService.page(page, queryWrapper);
        BeanUtils.copyProperties(usersPage,userPageVO);
        return ResultUtil.success(userPageVO);
    }

    @Override
    public Result<SystemSetting> querySystemSetting() {
        SystemSetting systemSetting = new SystemSetting();
        BeanUtils.copyProperties(systemProperties, systemSetting);
        return ResultUtil.success(systemSetting);
    }

    @Override
    public Result<Users> updateUserInfo(Request<Users> request) {
        boolean b = usersService.updateById(request.getBody());
        if(!b) {
            return ResultUtil.failure(StatusCode.USER_INFO_UPDATE_FAILED);
        }
        return ResultUtil.success(request.getBody());
    }

    @Override
    public Result<UserPlatformBindInfo> updatePlatformInfo(Request<UserPlatformBindInfo> request) {
        boolean b = userPlatformBindInfoService.updateById(request.getBody());
        if(!b) {
            return ResultUtil.failure(StatusCode.USER_INFO_UPDATE_FAILED);
        }
        return ResultUtil.success(request.getBody());
    }
}
